/**
 * 剑指 Offer 61. 扑克牌中的顺子
 * https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof/
 */

/**
 * @param {number[]} nums
 * @return {boolean}
 */
function isStraight(nums) {
  const cache = new Array(14);
  let min = Infinity;
  let max = -Infinity;
  let zeroCount = 0;
  for (let i = 0; i < nums.length; i += 1) {
    if (!nums[i]) {
      zeroCount += 1;
      continue;
    }
    if (cache[nums[i]]) {
      return false;
    }
    cache[nums[i]] = true;
    min = Math.min(min, nums[i]);
    max = Math.max(max, nums[i]);
  }
  for (let i = min + 1; i < max; i += 1) {
    if (!cache[i]) {
      if (!zeroCount) {
        return false;
      }
      zeroCount -= 1;
    }
  }
  return true;
}

console.log(isStraight([1, 2, 3, 4, 5]) === true);
console.log(isStraight([0, 0, 1, 2, 5]) === true);
console.log(isStraight([4, 7, 5, 9, 2]) === false);
